﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BdClient;
using Univer.Model;

namespace Univer
{
    public partial class ClientsForm : Form
    {
        private readonly BdExecute _execute;
        public ClientsForm()
        {
            _execute = new BdExecute();
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var table = new DataTable();
            table.Columns.Add("id");
            table.Columns.Add("имя");
            table.Columns.Add("Фамилия");
            table.Columns.Add("Отчество");
            table.Columns.Add("Пасспорт");
            table.Columns.Add("телефон");
            table.Rows.Clear();

            if (radioButton1.Checked)
            {
                foreach (var clientsResult in _execute.GetClients())
                {

                    table.Rows.Add(clientsResult.id,
                        clientsResult.Name, clientsResult.SecondName,
                        clientsResult.Patronymic, clientsResult.Passport, clientsResult.Phone);
                }
            } else if (radioButton3.Checked)
            {
                foreach (var clientsResult in _execute.GetConstantClients())
                {

                    table.Rows.Add(clientsResult.id,
                        clientsResult.Name, clientsResult.SecondName,
                        clientsResult.Patronymic, clientsResult.Passport, clientsResult.Phone);
                }
            }
            dataGridView1.DataSource = table;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = dataGridView1.SelectedRows.Count;
            if (count == 0)
            {
                MessageBox.Show("Не выбрано ни одного клиента");
            }
            else if (count > 1)
            {
                MessageBox.Show("Выбрано более одного клиента");
            }
            else
            {
                Client client = new Client();
                client.id = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[0].Value);
                client.name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                client.secondName = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                client.patronymic = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                client.passport = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[4].Value);
                client.phone = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[5].Value);
                Manager.getInstance().result = client;
                Manager.getInstance().mainFormCallback.updateClientInfo();
                Close();
            }
        }
        
    }
}
